layui.define(['table', 'form', 'laypage', 'laydate', 'layer', 'carousel'], function (exports) {
        var $ = layui.$
            , table = layui.table
            , form = layui.form
            , laydate = layui.laydate
            , laypage = layui.laypage
            , layer = layui.layer
            , carousel = layui.carousel;

        //路径
        var curWwwPath = window.document.location.href;
        var pathName = window.document.location.pathname;
        var pos = curWwwPath.indexOf(pathName);
        var localhostPaht = curWwwPath.substring(0, pos);
        var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);

        var limitcount = 10;
        table.render({
            elem: '#chemicalTable'
            , url: localhostPaht + projectName + "/rest/base/getXzglList"
            , toolbar: '#chemicalOperation'
            , height: 'full-200'
            , title: '村庄信息表'
            , defaultToolbar: ['filter', 'print']
            , cols: [[
                {title: 'ID', width: 70, sort: true, fixed: 'left', type: 'numbers'},
                {field: 'zhen', title: '乡镇名称', sort: true},
                {field: 'cunweihui', title: '村委会名称', sort: true},
                {field: 'name', title: '自然村名称', sort: true},
                {field: 'createTimeString', title: '创建时间', sort: true},
                {field: 'hushu', title: '户数', sort: true},
                // {field: 'jingdu', title: '经度', sort: true},
                // {field: 'weidu', title: '纬度', sort: true},
                // {field: 'contact', title: '联系人', sort: true},
                // {field: 'contactphone', title: '联系人电话', sort: true},
                {title: '操作', toolbar: '#table-chemical', align: "center", width: 250, fixed: "right"}]]
            , page: false
            , limit: limitcount
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            },
            // limits:[10, 20, 30, 40, 50],
            done: function (res, curr, count) {
                if (res.errorMsg == null) {

                } else {
                    layer.msg(res.errorMsg);
                }
                var curnum = $("#curnum").val();
                $('.layui-table-fixed-r').removeClass('layui-hide');//列固定右侧
                laypage.render({
                    elem: 'laypage'
                    , first: '首页'
                    , last: '尾页'
                    , groups: 5
                    , count: count
                    , curr: curnum
                    , limit: limitcount
                    , layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
                    , limits: [10, 15, 20]
                    , jump: function (obj, first) {
                        if (!first) {
                            curnum = obj.curr;
                            $("#curnum").val(curnum)
                            limitcount = obj.limit;
                            table.reload('chemicalTable', { //表格的id
                                where: {
                                    page: curnum,
                                    limit: limitcount
                                }
                            })

                        }
                    }
                })
            }
        })


        //监听工具条
        table.on('tool(chemicalTables)', function (obj) {  //表格的lay-filter="userlists"
            var data = obj.data; //获得当前行所有数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (obj.event === 'del') {
                deletes(data, obj);
            } else if (obj.event === 'update') {
                update(data);
            }
        });

    function deletes(data, obj) {
        layer.confirm('确定删除此数据', {icon: 3, title: '提示信息'}, function (index) {
            var index = layer.msg('删除中，请稍候', {icon: 16, time: false, shade: 0.6});
            setTimeout(function () {
                //向服务端发送删除指令
                $.ajax({
                    type: "post",
                    url: localhostPaht + projectName + "/rest/base/delxz",
                    data: "tid=" + data.tid,
                    dataType: "json",
                    async: false,
                    success: function (msg) {
                        layer.close(index);
                        if (msg === true) {
                            layer.msg('删除成功');
                            obj.del();
                        } else {
                            layer.msg(msg.errorMsg);
                        }
                    }
                })
            }, 2000);
        });
    }

        function update(data) {
            var clocess = "1";
            layer.open({
                title: "修改",
                type: 2,
                content: localhostPaht + projectName + '/rest/getRoute?type=instrument&url=xzgl_update',
                //是否显示右上角关闭图标，默认为1，显示
                area: ['90%', '91%'],
                maxmin: true,
                shade: 0.6,
                anim: 4,
                skin: 'layui-layer-molv',
                resize: true,
                success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.child(data.tid)
                }, cancel: function (index, layero) {
                    layer.close(index)
                    clocess = "2"
                    return false;
                },
                end: function () {
                    if (clocess !== "2") {
                        table.reload('chemicalTable')
                    }
                    return false;
                }
            });
        }

        function info(data) {
            layer.open({
                title: "详情",
                type: 2,
                content: localhostPaht + projectName + '/rest/getRoute?type=instrument&url=village_info',
                //是否显示右上角关闭图标，默认为1，显示
                area: ['90%', '91%'],
                maxmin: true,
                shade: 0.6,
                anim: 4,
                skin: 'layui-layer-molv',
                resize: true,
                success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                    var iframe = window['layui-layer-iframe' + index];
                    iframe.child(data.tid)
                }
            });
        }

    function binding(data) {
        var clocess = "1";
        layer.open({
            title: "绑定DTU",
            type: 2,
            content: localhostPaht + projectName + '/rest/getRoute?type=instrument&url=village_binding',
            //是否显示右上角关闭图标，默认为1，显示
            area: ['90%', '91%'],
            maxmin: true,
            shade: 0.6,
            anim: 4,
            skin: 'layui-layer-molv',
            resize: true,
            success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                var iframe = window['layui-layer-iframe' + index];
                iframe.child(data.tid);
            }, cancel: function (index, layero) {
                layer.close(index)
                clocess = "2"
                return false;
            },
            end: function () {
                if (clocess !== "2") {
                    table.reload('chemicalTable')
                }
                return false;
            }
        });
    }

    function unbound(data) {
        layer.confirm('确定解除该村庄对DTU的绑定？', {icon: 3, title: '提示信息'}, function (index) {
            var index = layer.msg('解绑中，请稍候', {icon: 16, time: false, shade: 0.6});
            setTimeout(function () {
                //向服务端发送删除指令
                $.ajax({
                    type: "post",
                    url: localhostPaht + projectName + "/rest/base/unboundVillage",
                    data: "tid=" + data.tid,
                    dataType: "json",
                    async: false,
                    success: function (msg) {
                        layer.close(index);
                        if (msg === true) {
                            layer.msg('解绑成功');
                        } else {
                            layer.msg(msg.errorMsg);
                        }
                        table.reload('chemicalTable')
                    }
                })
            }, 2000);
        });
    }

        exports('village', {})
    }
)
;